#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

plugin_name="$1"
pkg_name="$2"
plugin_subdir="$3"
release_msg="$4"
pkgbuild_path="${5:-packaging/arch/PKGBUILD_${plugin_name}}"

aur_dev_repo_root=~/build/

cp "$pkgbuild_path" "${aur_dev_repo_root}/${pkg_name}/PKGBUILD"
cd "${aur_dev_repo_root}/${pkg_name}/"
updpkgsums ; makepkg -fsr ; makepkg --printsrcinfo > .SRCINFO
git diff
echo
echo "??????????????????????????????????????????????????"
echo "??    Confirm push to '${pkg_name}' AUR? [y/N]    ??"
echo "??????????????????????????????????????????????????"
echo -n "> "
read -r answer
echo
if [[ "${answer}" = "y" ]] ; then
	git add PKGBUILD .SRCINFO
	git commit -m "push version (${release_msg})"
	git push origin HEAD
fi

cd -
cp "${aur_dev_repo_root}/${pkg_name}/PKGBUILD" "$pkgbuild_path"
echo
echo "???????????????????????????????????????????????????????????????????????????????????????"
echo "??    Confirm pushing new version of '${plugin_name}' to Oomox GitHub repo? [y/N]    ??"
echo "???????????????????????????????????????????????????????????????????????????????????????"
echo -n "> "
read -r answer
echo
if [[ "${answer}" = "y" ]] ; then
	plugin_dir_path="plugins/${plugin_name}${plugin_subdir}"
	if [[ "$pkgbuild_path" == plugins/* ]] ; then
		relative_pkgbuild_path=${pkgbuild_path#"$plugin_dir_path"}
		git -C "$plugin_dir_path" add "$relative_pkgbuild_path"
		git -C "$plugin_dir_path" commit -m "chore: push release"
		git -C "$plugin_dir_path" push ssh HEAD
		#git -C "$plugin_dir_path" pull origin master
	else
		git add "$pkgbuild_path"
	fi
	git add "$plugin_dir_path"
	git commit -m "chore(plugins: ${plugin_name}): update submodule (${release_msg})"
	git push origin HEAD
fi

echo
echo '$$$$$$$$$$$$$$$$$$$$$$$$$'
echo '$$    Full Success!    $$'
echo '$$$$$$$$$$$$$$$$$$$$$$$$$'
exit 0
